<template>
  <div>
    <van-tabs v-model="active">
      <van-tab title="主体信息">
        <div class="page_space">
          <p class="title_1">基本信息</p>
          <base-card v-if="baseInfo.length > 0" :data="baseInfo" />

          <p class="title_1">关联档口</p>
          <base-card
            v-for="(item, index) in relationStall"
            :key="index"
            :data="item.data"
            :title="item.title"
            class="mb-15"
          />
          <card v-if="relationStall.length === 0">
            <p class="text-center empty">暂无信息</p>
          </card>
        </div>
        <div class="btn_wrap">
          <button class="btn" @click="toInspect">立即采样</button>
        </div>
      </van-tab>

      <van-tab title="快检记录">
        <div class="page_space">
          <base-card
            v-for="(item, index) in records"
            :key="index"
            :data="item.data"
            :title="item.title"
            :collapse="true"
            emptyStr="待检测"
            class="mt-15"
          />
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import card from '@/components/base-card/wrap'
import baseCard from '@/components/base-card'

import {
  getInspectRecord,
  getSubjectDetailByCode,
  newCorrespondent
} from '@/api/subject'
export default {
  name: 'samplingSubjectDetails',
  components: { card, baseCard },
  data() {
    return {
      active: 0,
      subject: {},
      baseInfo: [],
      relationStall: [], // 关联档口
      records: [] // 快检记录
    }
  },
  created() {
    let subjectId = this.$route.query.id
    // let subjectId = '129000420100000000059'
    this.getInfo(subjectId)
    // this.getInspectRecord(subjectId)
  },
  methods: {
    // 获取快检记录
    async getInspectRecord(subjectId) {
      let params = {
        buildControls: 'BUILD_REPORT_INFO,BUILD_SAMPLE_SORT_INFO',
        businessVarietyName: '',
        intercourseEnterpriseId: subjectId,
        limit: 10,
        page: 1,
        stateNotIn: 'DRAFT,CANCEL,DELETED'
      }
      const { code, data } = await getInspectRecord(params)
      if (code === '0') {
        data.content.forEach((item) => {
          // console.log(item.reportInfoDTOList)
          let rows = []
          item.reportInfoDTOList.forEach((row) => {
            // 快检记录
            if (row.reportDetailInfoDTOList.length > 0) {
              rows.push({
                key: row.reportDetailInfoDTOList[0].sampleProjectInfoDTO.name,
                value: row.reportDetailInfoDTOList[0].singleResultText,
                danger:
                  row.reportDetailInfoDTOList[0].singleResultText === '阳性'
              })
            }
          })
          this.records.push({
            title: `${item.businessVarietyNames} ${item.objectModificationTime}`,
            id: item.id,
            data: rows
          })
        })
        // console.log(this.records)
      }
    },
    async getInfo(id) {
      const { code, data } = await getSubjectDetailByCode(id)
      if (code === '0') {
        // this.subject = data
        // // 主体信息
        // this.baseInfo = [
        //   {
        //     key: '主体类型',
        //     value: data.subjectTypeText
        //   },
        //   {
        //     key: '主体名称',
        //     value: data.name
        //   },
        //   {
        //     key: '营业执照/统一社会信用代码/身份证',
        //     value: data.uscId
        //   },
        //   {
        //     key: '所属区划',
        //     value: data.allAreaName
        //   },
        //   {
        //     key: '所属市场',
        //     value: data.belongEntName
        //   },
        //   {
        //     key: '经营状态',
        //     value: data.manageStateText
        //   },
        //   {
        //     key: '主体性质',
        //     value: data.subjectNatureText
        //   },
        //   {
        //     key: '负责人姓名',
        //     value: data.legalPerson
        //   },
        //   {
        //     key: '联系人姓名',
        //     value: data.contacts
        //   },
        //   {
        //     key: '联系方式',
        //     value: data.contactsTel
        //   },
        //   {
        //     key: '经营范围',
        //     value: data.varieties || '暂无'
        //   }
        // ]
        // // 关联档口
        // let { relationPlaceInfoList } = data
        // relationPlaceInfoList.forEach((item) => {
        //   this.relationStall.push({
        //     title: item.placeName,
        //     data: [
        //       {
        //         key: '档口状态',
        //         value: item.businessStatusText
        //       },
        //       {
        //         key: '档口编号',
        //         value: item.placeCode
        //       }
        //     ]
        //   })
        // })
        // console.log('hh', data.certificatePicInfoJson)
        // 新增往来单位
        const { code: code2, data: data2 } = await newCorrespondent({
          id: data.id,
          certificatePicInfoJson: JSON.stringify(data.certificatePicList), //
          varietyTypeJson: JSON.stringify(data.varietyTypeList) // varietyTypeList 转json
        })
        if (code2 === '0') {
          this.$router.replace('/subject-details?id=' + data2)
        }
        //   certificatePicInfoJson: JSON.stringify(certificatePicList),
        // varietyTypeJson: JSON.stringify(varietyTypeList)
      } else {
        this.$router.go(-1)
      }
    },
    // 立即采样
    toInspect() {
      this.$router.push({
        name: 'immediateSampling',
        query: {
          intercourseEnterpriseInfo: encodeURIComponent(
            JSON.stringify({
              intercourseEnterpriseId: this.subject.id, // 经营主体id
              intercourseEnterpriseName: this.subject.name, // 经营主体
              subjectType: this.subject.subjectType, // 主体类型
              subjectTypeText: this.subject.subjectTypeText, // 主体类型 中文
              managementEntName: this.subject.managementEntName // 所属市场
            })
          )
        }
      })
    }
  }
}
</script>

<style lang="scss">
.empty {
  color: #656c74;
}
</style>
